package pro.softzhang.algo.lc600;

import pro.softzhang.algo.flag.x.XOptimal;

/**
 *
 */
public class LC605_CanPlaceFlowers {
    public static void main(String[] args) {
        int[] flowered = {1, 0, 0, 0, 1};
        int n = 1;
        System.out.println(new Solution().canPlaceFlowers(flowered, n));
    }

    @XOptimal
    static
    class Solution {
        public boolean canPlaceFlowers(int[] flowerbed, int n) {
            for(int i = 0; i < flowerbed.length && n > 0;) {
                if (flowerbed[i] == 1) {
                    i += 2;
                } else if (i == flowerbed.length - 1 || flowerbed[i + 1] == 0) {
                    n--;
                    i += 2;
                } else {
                    i = i + 3;
                }
            }
            return n <= 0;
        }
    }
}
